package ru.batrdmi.svnplugin;

import com.intellij.openapi.components.*;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;

@State(
  name = SVNRevisionGraphConfiguration.COMPONENT_NAME,
  storages = {@Storage(id = "other", file = "$PROJECT_FILE$"),
              @Storage(id = "dir", file = "$PROJECT_CONFIG_DIR$/other.xml", scheme = StorageScheme.DIRECTORY_BASED)}
)
public class SVNRevisionGraphConfiguration implements ProjectComponent, Configurable,
        PersistentStateComponent<SVNRevisionGraphConfiguration.State> {
    public static SVNRevisionGraphConfiguration getInstance(Project project) {
        return project.getComponent(SVNRevisionGraphConfiguration.class);
    }

    public static final String COMPONENT_NAME = "SVNRevisionGraph.Configuration";
    private State state = new State();
    private JPanel myPanel;
    private JCheckBox myCheckBox;

    @Override
    public void initComponent() {
    }

    @Override
    public void disposeComponent() {
    }

    @NotNull
    @Override
    public String getComponentName() {
        return COMPONENT_NAME;
    }

    @Nls
    @Override
    public String getDisplayName() {
        return "SVN Revision Graph";
    }

    @Override
    public Icon getIcon() {
        return null;
    }

    @Override
    public String getHelpTopic() {
        return null;
    }

    @Override
    public void projectOpened() {
    }

    @Override
    public void projectClosed() {
    }

    @Override
    public JComponent createComponent() {
        myPanel = new JPanel();
        myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));
        myCheckBox = new JCheckBox("Enable explicit scan over all current branches/tags");
        myPanel.add(myCheckBox);
        return myPanel;
    }

    @Override
    public boolean isModified() {
        return myCheckBox.isSelected() != state.extendedScanEnabled;
    }

    @Override
    public void apply() throws ConfigurationException {
        state.extendedScanEnabled = myCheckBox.isSelected();
    }

    @Override
    public void reset() {
        myCheckBox.setSelected(state.extendedScanEnabled);
    }

    @Override
    public void disposeUIResources() {
        myCheckBox = null;
        myPanel = null;
    }

    public boolean isExtendedScanEnabled() {
        return state.extendedScanEnabled;
    }

    public void setExtendedScanEnabled(boolean enabled) {
        state.extendedScanEnabled = enabled;
    }

    @Override
    public State getState() {
        return state;
    }

    @Override
    public void loadState(State state) {
        this.state = state;
    }

    public static class State {
        public boolean extendedScanEnabled;
    }
}
